<?php
	include_once(dirname(__FILE__) . "/model/nms_alarm_model.php");
	include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
	include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
	function draw_log_device(){
		html_master_script_filter();
		html_log_device_script();
		draw_log_device_header();
	//xac dinh bao nhiu dong
	$display =50;
	//tinh so trang can hien thi
	//begin get query string from url to decide what relevant data
		$sql_where  ="";
		/* if the user pushed the 'clear' button */
		if (isset($_REQUEST["clear_x"])) {
			unset($_REQUEST["host_id"]);
			unset($_REQUEST["search"]);
			unset($_REQUEST["filter"]);
			$_REQUEST["id"]=0;
			$_REQUEST['run']='alarm';
			
		}
		/* get value from action filter host*/
		if (!empty($_GET['host_id'])) {	
			if($_GET['host_id']=='null')
			{
			$sql_where.="";	
			}else{
			$sql_where .="and a.id =".$_GET['host_id']."";
			}
		}
		/* get value from action filter status*/
		if(isset($_REQUEST['filter'])){
			if($_REQUEST['filter']=="All"){
			$sql_where .="";
			}
			if($_REQUEST['filter']=="Up"){
			$sql_where.=" and b.host_status='3'";
			}
			if($_REQUEST['filter']=="Down"){
				$sql_where.=" and b.host_status='1'";
			}
			if($_REQUEST['filter']=="Unknown"){
				$sql_where.=" and b.host_status>3";
			}
			if($_REQUEST['filter']=="Recovering"){
				$sql_where.=" and b.host_status ='2'";
			}
		}
		/* get value from action search information*/
		if (!empty($_REQUEST['search'])) {
			$research=trim($_REQUEST['search']);
						$sql_where.="and ( a.description like '%".$research."%' or a.hostname like '%".$research."%')";
		}
		/* Get total interface_id from table plugin_nms_interface*/
			if(empty($_GET['record']))
			{
				$getrecord=50;
			}else{
				$getrecord=$_GET['record'];
			}		
		$query="SELECT count(b.hostlog_id) FROM (SELECT b.hostlog_id FROM plugin_nms_hostlog b ,host a
				where b.host_id=a.id ".$sql_where."
				limit 0,$getrecord) as b";
		$record =db_fetch_cell($query);
		if ($record > $display){
			$page = ceil($record/$display);
		}else {
			$page = 1;
		}
		if(!empty($_REQUEST['id'])){
			$id=$_REQUEST['id'];
		}else{
			$id=0;
		}
	$start = $id;
	$current = ($start/$display)+1;
	$next = $start + $display;
	$previous = $start - $display;
	$last = ($page - 1)*$display;
	if ($current >= 11) {
		$start_page = $current - 5;
		if ($page > $current + 5)
			$end_page = $current + 5;
		else if ($current <= $page && $current > $page - 10) {
			$start_page = $page - 10;
			$end_page = $page;
		} else {
			$end_page = $page;
		}
	} else {
		$start_page = 1;
		if ($page > 11)
			$end_page = 11;
		else
			$end_page = $page;
	}
	$querydata="SELECT b.hostlog_id, a.hostname ,a.description, b.host_status ,b.host_time 
					FROM plugin_nms_hostlog b ,host a
					where b.host_id=a.id  ".$sql_where."
					order by b.host_time DESC limit $start,$display";
	draw_log_device_nav($page,$previous,$display,$next,$last,$current,$start_page,$end_page);
	draw_log_device_table($querydata);
	if (isset($_GET['search'])) {
		?>
					<script >
						var tmp = $('#id_master_table tr').removeHighlight().highlight('<?php echo trim($_GET['search']) ?>');
						alert(test);
						$('#id_master_table').empty();
						$('#id_master_table').html(test);
					</script>
					<?php 
					}
	draw_log_device_nav($page,$previous,$display,$next,$last,$current,$start_page,$end_page);
	html_master_footer_1();
 	}
 	 
 	function draw_log_device_header(){
 	html_master_header_start("Host Load Filter");
?>
    		<table width="100%" border="0">
    			<tr>
	    		<!-- Status filter -->
	    			<td width="3%">Status:</td>
	    			<td width="5%">
		    			<select onchange="val()"  id="id_status_alarm1">
				        	<option value="All">All</option>
				            <option value="Up">Up</option>
				            <option value="Down">Down</option>
				            <option value="Unknown">Unknown</option>
				            <option value="Recovering">Recovering</option>
		        		</select>
	    			</td>
	    			<!--  -->
	    			<!-- Devices filter -->
	    			<td width="3%" >Device:</td>
		    		<td>
		    			<select  onchange="val()" id="id_host_alarm1">
		    				<option value="">Any</option>
			        		<?php
	     					 $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host where description !='Localhost' order by description,hostname");      $result3 = db_fetch_assoc($sql54);
	     					 if (sizeof($hosts) > 0) {
						     foreach($hosts as $set) {
						     echo "<option value='" . $set["id"] . "'"; 
						     if(isset($_REQUEST["host_id"])){if($_REQUEST["host_id"] == $set["id"]){echo "selected";}};
						     echo ">" . htmlspecialchars($set["name"]) . "</option>\n";
						     }
						     }
	   						?>
		        		</select>
		        		<script type="text/javascript">
						$("#id_host_alarm1").chosen({
							search_contains: true,
							no_results_text:"Oops, nothing found!"});
						$("#id_host_alarm1").on('change',function(){
							var statusValue = $(this).val();
						 	var deviceValue = $("#id_host_alarm1").val();
						 	 var searchFilter = $("#id_search1").val();
						 	var record= $("#id_status_alarm2").val();
						 	//build query string
						 	var queryString = "run=log&tab=log_device&id=0&filter="+statusValue+"&host_id="+deviceValue+"&record="+record+"&search="+searchFilter;
						 	document.location.href = "?"+queryString;
						});
 					 </script>
		    		</td>
	    			<!--  -->
	    			<!-- Button export -->
	  				<!-- Search -->
					<td>Search</td>
					<td colspan="3" scope="col">
	    				<input id="id_search1" type="search" onkeypress="keydown(event)"/>
	    			</td>
	    			<td>&nbsp;</td>
			    	<td>
			    		<input id="btnGo1" type="button" value="Go" ;">
			    	</td>
			    	<td>&nbsp;</td>
			    	<td>
			    		<input id="btnClear1" type="submit" name="clear_x" value="Clear" title="Clear Filters">
			    	</td>
			    	<td>&nbsp;</td>
			    	<td>
			    		<input id="btnExport1" type="button" value="Export" onclick="location.href='';">
			    	</td>
			    	<td>&nbsp;</td>
					<td>
						<input id="btnRefresh1" type="button" value="Refresh" onclick="location.href='';">
					</td>
					<td width="3%">Record:</td>
	    			<td width="5%">
		    			<select onchange="val()"  id="id_status_alarm2">
		    				<option value="50">Default</option>
				        	<option value="100">100</option>
				            <option value="200">200</option>
				            <option value="500">500</option>
				            <option value="1000">1000</option>
				            <option value="2000">2000</option>
				            <option value="3000">3000</option>
		        		</select>
	    			</td>
			    </tr>
    		</table> 		
   	<?php 
   	html_master_header_end();
 	}
 	function draw_log_device_nav($page,$previous,$display,$next,$last,$current,$start_page,$end_page){
 		html_master_nav_page_start();
 		if (empty($_GET['filter'])) {
 			$status="All";
 		}
 		else{
 			$status =  $_GET['filter'];
 		}
 		if (empty($_GET['host_id'])) {
 			$host_id="";
 		}
 		else{
 			$host_id =  $_GET['host_id'];
 		}
 		if (empty($_GET['record'])) {
 			$record1="50";
 		}
 		else{
 			$record1 =  $_GET['record'];
 		}
 		if (empty($_GET['search'])) {
 			$search="";
 		}
 		else{
 			$search =  $_GET['search'];
 		}
 		
 		?>
 		    <script type="text/javascript">
 		    	$("#id_status_alarm1").val('<?php echo $status;?>');
 		    	$("#id_host_alarm1").val('<?php echo $host_id;?>');
 		    	$("#id_status_alarm2").val('<?php echo $record1;?>');
 		    	$("#id_search1").val('<?php echo $search;?>');
 		    </script>
 		    <?php 
 		    		if (isset($_REQUEST["btnClear"])) {
 				    	unset($_REQUEST["#id_search"]);
 				    	unset($_REQUEST["id_host_alarm"]);
 				    	unset($_REQUEST["id_status_alarm"]);
 		    		}
 		    		//build where status query
 		    		if(isset($_GET['filter']) or isset($_GET['host_id']) or isset($_GET['search']) or isset($_GET['record'])){
 		    		 	$base_url = "&filter=".$status."&host_id=".$host_id."&record=".$record1."&search=".$search."";
 		    		}
 		    		 else{
 		       		$base_url=null;
 		       		}
 			  	   	if($page > 1) {
 			       	if ($current > 1) {
 			            echo "<li><a href='nms.php?run=log&tab=log_device&id=0$base_url'>First</a></li>";
 			            echo "<li><a href='nms.php?run=log&tab=log_device&id=$previous$base_url'>Previous</a></li>";
 			       	} 
 			       for ($i = $start_page; $i <= $end_page; $i++) {
 			 
 			            if ($current == $i)
 			                echo "<li class='current'>$i</li>";
 			            else
 			                echo "<li><a href='nms.php?run=log&tab=log_device&id=".($display*($i-1))."$base_url'>$i</a></li>";
 			        }
 			      	if ($current < $page) {
 			            echo "<li><a href='nms.php?run=log&tab=log_device&id=$next$base_url'>Next</a></li>";
 			            echo "<li><a href='nms.php?run=log&tab=log_device&id=$last$base_url'>Last</a></li>";
 			           } 
 			     	}	     
 		html_master_nav_page_end();
 	}
 	function draw_log_device_table($querydata){
		html_master_table_start("100%", "center");
 	?>
  			<tr class="cls_master_table_title" align="center">
  				<th width="3%" scope="col">No</th>
			    <th width="15%" scope="col">Description</th>
			    <th width="30%" scope="col">Hostname</th>
			    <th width="6%" scope="col">Status</th>
			    <th width="12%" scope="col">Time</th>
  			</tr>
  			
  				<!-- play sound when devices down -->
					<?php
					?>		
  					<!-- do du lieu vao bang -->
   					<?php 
					$result = db_fetch_assoc($querydata);
					$stt =0;
					foreach($result as $set) {
					//$id1 = $set['id'];
					?>
					<!-- thay doi mau cho cac trang thai trong bang -->
					<tr  <?php
					 if($set['host_status']=='1') {echo 'class="mautrangthaiDown"';}
					 elseif($set['host_status']=='2'){echo 'class="mautrangthaiUnknow1"';}
					 elseif($set['host_status']=='3'){echo ' class="mautrangthaiUp"';}			
					 elseif($set['host_status']>'3'){echo 'class="mautrangthaiUnknow"';}
					 ?>>
					  	<td align="center" id="getpost">&nbsp;<?php echo $set['hostlog_id']?></td>
					    <td >&nbsp;<?php echo $set['hostname']?></td>
					    <td >&nbsp;<?php echo $set['description']?></td> 
					    <td align="center">&nbsp;<?php
					     if($set['host_status']=='1') {echo 'Down';}
					 	elseif($set['host_status']=='3'){echo 'Up';}
					 	elseif($set['host_status']>'3'){echo 'Unknow';}
					 	elseif($set['host_status']=='2'){echo 'Recovering';} ?></td> 
					    <td align="center">&nbsp;<?php  echo $set['host_time'] ?></td>				    	
	  				</tr>
				<?php		   
				}
				?>
			</table>
	</div>
<?php 	
 	}
 	function html_master_footer_1(){
?>
 	  	 		<div id="center_footer_up">
 					  	 	 			<div class="countdown_time"  >
 					  	 	 				<h3><div id="Timerefresh">Refresh time: </div></h3>
 					  	 	 				<h3><div id ="countdown_time2"></div></h3>
 					  	 	 			</div>
 				</div>
<?php 	
 	  	html_alarm_script_time();
 	  }
?>
 	 	